package com.jic.filecenter.controller;

import com.jic.common.base.vo.RestResult;
import com.jic.filecenter.common.FileUtil;
import com.jic.filecenter.controller.errors.FileCenterErrorCode;
import com.jic.filecenter.controller.errors.FileCenterException;
import com.jic.filecenter.service.upload.Alioss;
import com.jic.filecenter.service.upload.FileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Slf4j
public class FileUploadController {

    @Value("${file.upload.path}")
    private String filePaht;

    @Autowired
    private FileUploadService fileService;

    @Autowired
    private Alioss alioss;

    /**
     * 文件上传到本地服务器
     * @param file
     * @param type
     * @return
     * @throws Throwable
     */
    @PostMapping("/upload")
    public RestResult<Map> uploadFile(@RequestParam("file") MultipartFile file,
                                 @RequestParam(value = "type",required = false) String type) throws Throwable{

        log.debug("uploading file size: " + String.valueOf(file.getSize()) + " type: " + file.getContentType());
        RestResult<Map> restResult = fileService.fileUpload(file,type);
        return restResult;

    }

    /**
     * 文件上传到阿里云OSS服务器
     * @param file
     * @param type
     * @return
     * @throws Throwable
     */
    @PostMapping("/ali/upload")
    public RestResult<String> aliUpaload(@RequestParam("file") MultipartFile file,
                                 @RequestParam(value = "type",required = false) String type) throws Throwable {

        log.debug("uploading file size: " + String.valueOf(file.getSize()) + " type: " + file.getContentType());
        if(file.getSize() < 10240||file.getSize() > 204800){
            throw  new FileCenterException(FileCenterErrorCode.FILE_SIZE_RANGE);
        }
        String resourceFilename = file.getOriginalFilename();
        String suffix = resourceFilename.substring(resourceFilename.lastIndexOf("."),resourceFilename.length());
        if(!suffix.equals(".jpg") && !suffix.equals(".jpeg") && !suffix.equals(".bmp") && !suffix.equals(".png")){
            throw  new FileCenterException(FileCenterErrorCode.FILE_FORMA_ERROR);
        }
        try {
            String url = alioss.filelUpload(file,type);
            return RestResult.success(url);
        } catch (IOException e) {
            log.error("File Upload Fail",e);
            throw  new FileCenterException(FileCenterErrorCode.FILE_UPLAOD_ERROR);
        }
    }

    /**
     * 从本地服务器下载文件
     * @param filename
     * @throws Throwable
     */
    @GetMapping("/download")
    public void download(@RequestParam("fileName") String filename ) throws Throwable {
        fileService.fileDownload(filename);
    }
}
